public class ArrayDegree {
    public int findShortestSubArray(int[] nums) {
        Map<Integer, int[]> info = new HashMap<Integer, int[]>();
        for (int i = 0; i < nums.length ; i ++) {
            if (info.containsKey(nums[i])) {
                info.get(nums[i])[0]++;
                info.get(nums[i])[2] = i;
            } else {
                info.put(nums[i], new int[]{1, i, i});
            }
        }
        int max = 0; int minL = 0;
        for (int[] arr : info.values()) {
            if (max < arr[0]) {
                max = arr[0];
                minL = arr[2] - arr[1] + 1;
            } else if (max == arr[0]) {
                minL = Math.min(minL, (arr[2] - arr[1] + 1));
            }
        }
        return minL;
    }
}